package com.syqc.monitor.ui.sms

import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/**
 *@Author Jasper
 *@Time   2021/1/12
 *@Desc   发送短信指令
 */
class SMSSendViewModel : AutoDisposeViewModel() {
    var carId = ""
    var sendMsg = ""
    var simNum = ""

    private val simSub by lazy { PublishSubject.create<String>() }
    fun simObserver(): Observable<String> = simSub.hide()

    /**
     * 通过carId查询SIM卡号
     */
    fun getSimByCarId() {
        toastLoading(true)
        ServiceHelper.getRepository().getCarInfo(carId).autoDispose(this).subscribe({
            toastLoading(false)
            val simCard = it.sim
            if (simCard.isNullOrEmpty()) {
                toast(R.string.toast_no_about_sim)
                return@subscribe
            }
            simSub.onNext(simCard)
        }, error)
    }

    /**
     * 判断参数
     */
    fun isParamsOk(): Boolean {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return true
        }
        if (sendMsg.isEmpty()) {
            toast(R.string.hint_toast_send_content)
            return true
        }
        if (simNum.isEmpty()) {
            toast(R.string.hint_toast_sim)
            return true
        }
        return false
    }
}
